package org.devops

def get_mysql_msg_map(String dbName, String tableName) {
    def resultMap = [:] // 创建一个空的 Map 用于存储结果

    getDatabaseConnection(type: 'GLOBAL') {
        def result = sql(sql: "SELECT * FROM `${dbName}`.`${tableName}`;")
        
        if (result) {
            println "查询结果: ${result}" // 调试输出
            result.each { row ->
                row.each { key, value ->
                    resultMap[key] = value // 将字段名作为键，字段值作为值
                }
            }
        } else {
            println "查询没有返回任何结果"
        }
    }
    
    return resultMap // 返回结果 Map
}

// -----
// 拉取job在数据库中的信息
def get_mysql_msg(jobName) {
    println "===========获取${jobName}信息============"
    getDatabaseConnection(type: 'GLOBAL') {
        def result = sql("SELECT *  FROM `job_var` WHERE job_name=\"${jobName}\"")
        return result
    }
}

// 格式化数据信息，返回的是map信息
def get_job_msg (res) {
    message = ""
    for (i in res) {
        if (i != "[" && i != "]") {
            message += i
        }
    }
    job_map = message.split(', ').collectEntries{it.replaceFirst(/:/, '000000').minus("]").minus("[").split('000000') as List}
    return job_map
}
// -----


// 向数据库中插入本次构建记录，需要创建表并创建对应字段
def set_job_history (buildId,platForm,jobName,branch,buildImage,messAge,build_user) {
    getDatabaseConnection(type: 'GLOBAL') {
        def result = sql("INSERT INTO `job_histoy` SET build_id='" + buildId + "',platform= '" + platForm + "',job_name= '" + jobName + "',branch= '" + branch + "',build_image= '" + buildImage + "',build_user='" + build_user + "',message= '" + messAge + "';")
    }
}




//CREATE TABLE `HelloWorld-SqlPipeline` (
//  `git_repo_url` varchar(255) DEFAULT NULL,
//  `registry_url` varchar(255) DEFAULT NULL,
//  `harbor_url` varchar(255) DEFAULT NULL,
//  `project_group` varchar(255) DEFAULT NULL,
//  `project_name` varchar(255) DEFAULT NULL,
//  `container_name` varchar(255) DEFAULT NULL,
//  `ops_share_library` varchar(255) DEFAULT NULL
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
//--------------------
//CREATE TABLE `job_var` (
//  `job_name` varchar(255) DEFAULT NULL,
//  `git_repo_url` varchar(255) DEFAULT NULL,
//  `registry_url` varchar(255) DEFAULT NULL,
//  `harbor_url` varchar(255) DEFAULT NULL,
//  `project_group` varchar(255) DEFAULT NULL,
//  `project_name` varchar(255) DEFAULT NULL,
//  `container_name` varchar(255) DEFAULT NULL,
//  `ops_share_library` varchar(255) DEFAULT NULL
//) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
//--------------------
//INSERT INTO `HelloWorld-SqlPipeline` (`git_repo_url`, `registry_url`, `harbor_url`, `project_group`, `project_name`, `container_name`, `ops_share_library`) 
//VALUES 
//('git@gitee.com:wolfan_119/spring-boot-3-hello-world-jdk21.git', 'harbor.muscledog.top', 'https://harbor.muscledog.top', 'devops', 'hello-world-jdk21', 'hello-world-jdk21', 'git@gitee.com:wolfan_119/ops-share-librarya.git');
//--------------------
//INSERT INTO `job_var` 
//(`job_name`, `git_repo_url`, `registry_url`, `harbor_url`, `project_group`, `project_name`, `container_name`, `ops_share_library`) 
//VALUES 
//('HelloWorld-SqlPipeline', 'git@gitee.com:wolfan_119/spring-boot-3-hello-world-jdk21.git', 'harbor.muscledog.top', 'https://harbor.muscledog.top', 'devops', 'hello-world-jdk21', 'hello-world-jdk21', 'git@gitee.com:wolfan_119/ops-share-library.git');
